const fileService = require('../service/file.service')
const userService = require('../service/user.service')
const {
  APP_PORT,
  APP_HOST
} = require('../app/config')
class FileController {
  async saveAvatarInfo(ctx, next) {
    //1.获取图像相关的信息
    const {
      id
    } = ctx.user
    const {
      filename,
      mimetype,
      size
    } = ctx.req.file
    //2.将图像信息保存到数据库中
    const result = await fileService.createAvatar(filename, mimetype, size, id)
    //3.将图片地址保存到user表中
    const avatarUrl = `${APP_HOST}:${APP_PORT}/users/${id}/avatar`
    await userService.updateAvatarUrlById(avatarUrl, id)
    ctx.body = '上传头像成功'
  }
  async savePictureInfo(ctx,next){
    const files = ctx.req.files
    const {id} = ctx.user
    const {momentId} = ctx.query
    for (let file of files){
      const {filename,mimetype,size} = file
      await fileService.createFile(filename,mimetype,size,id,momentId)
    }
    ctx.body = '动态配图上传完成'
  }
}

module.exports = new FileController()